<?php
// +----------------------------------------------------------------------+
// | The Answern Inc                                                  |
// +----------------------------------------------------------------------+
// | Copyright (c) 2017, Answern Inc. All rights reserved.            |
// +----------------------------------------------------------------------+
// | Authors: Answern Inc.                    |
// |                                                                      |
// +----------------------------------------------------------------------+
 
/**
 * @version  1.0
 * @author   zxiaofee
 * @date     2017-05-11
 */

namespace App\Libs\Welcome;
use \App\Libs\Base\Constant;
use \App\Libs\Exception\RuntimeException;
use \App\Libs\DB\PailePdo;
class Welcome extends \App\Libs\Base\Singleton
{
    private $table = 't_paile_welcome';

    public function save($welcomeImgUrl = false , $url ) {
        if ($welcomeImgUrl === false) {
            return false;
        }
        $sql = 'select `id`, `imgurl`,url from `' . $this->table . '` limit 1';
        $welcome = PailePdo::instance()->find($sql);
        $ret = false;
        $values = array(
            'imgurl' => $welcomeImgUrl,
			'url' => $url ,
            'time'   => strval(time()),   
        );
        if (!$welcome) { //如果没有则插入一条
            return PailePdo::instance()->insert($this->table,$values);
        }
        //有则更新
        $conditions = array(
            'conditions'    => 'id=?',
            'bind'          => array('id' =>$welcome['id']),
        );

        return PailePdo::instance()->update($this->table, $values, $conditions);
    }

    public function get(){
        $sql = 'select `id`, `imgurl`,`url` from `' . $this->table . '` limit 1';
        $welcome = PailePdo::instance()->find($sql);
        if ($welcome == null) {
            $welcome['imgurl'] = '';
        }
        return array('list' =>  array( array( 'img' =>$welcome['imgurl'] , 'url'=> $welcome['url'] ) ) );
    }
}
